package com.ruoyi.app.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat
import com.drake.net.Post
import com.drake.net.utils.scopeNetLife
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.hjq.toast.ToastUtils
import com.ruoyi.app.R
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.databinding.ActivitySettingBinding
import com.ruoyi.code.base.BaseBindingActivity
import com.tencent.mmkv.MMKV
import com.xuexiang.xupdate.XUpdate

class SettingActivity : BaseBindingActivity<ActivitySettingBinding>() {

    companion object {

        fun startActivity(context: Context) {
            val intent = Intent(context, SettingActivity::class.java)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }

    }

    override fun initView() {
        binding.titlebar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })
        binding.llEditPwd.setOnClickListener {
            EditPwdActivity.startActivity(this)
        }
        binding.llClearCache.setOnClickListener {
            ToastUtils.show("模块建设中~")
        }
        binding.llUploadApp.setOnClickListener {
            XUpdate.newBuild(this)
                .promptThemeColor(ContextCompat.getColor(this, R.color.red))
                .updateUrl(ConfigApi.uploadApp)
                .update()
        }
        binding.tvLoginOut.setOnClickListener {
            scopeNetLife {
                Post<String>(ConfigApi.logout).await();
                MMKV.defaultMMKV().remove("token")
                LoginActivity.startActivity(this@SettingActivity)
            }
        }
    }

    override fun initData() {

    }
}